package Market;

import java.util.*;
import edu.iastate.jrelm.rl.SimpleStatelessLearner;
import sim.engine.*;


public class Market extends SimState
{
	private static final long serialVersionUID = 6628525522939315274L;
	
// GenCo part												
												/** number of GenCos */
	  private int I=5;	  
	  private double[][] GenCoData;
	  private double[][] LearningData;
	  private ArrayList GenCoList;
	  private double[][] TSupplyOffer;
	  private double[][] RSupplyOffer;
	  private ElGrid elgrid;
	  private ArrayList ADList; //action domain list
	  SimpleStatelessLearner Learner;
	  boolean Switch;
	  
// LSE part
												/** number of LSEs */
	  private int J=3;      
	  private double[][] LSEData;
	  private ArrayList LSEList;
	  private double[] LoadProfile;

// ISO part

	  private ArrayList LMPList;
//-------Constructor----------------------------------------------
    public Market(long seed)
        {
        super(seed);
        }
//-----------------------------------------------------------------
    
    //public void start(long RandomSeed)
    public void start()
        {
        super.start();
        	
        elgrid = new ElGrid();
        	
//----- GenCo--------------------------------------------------------               	
        	
        	//java.util.Random randomSeed = new java.util.Random((int)this.getRngSeed());
        	Switch		 = false;
        	ADList		 = new ArrayList();
        	GenCoList    = new ArrayList();
        	GenCoData    = new double[I][8];
        	LearningData = new double[I][12];
        	TSupplyOffer = new double[I][4];
        	RSupplyOffer = new double[I][4];
       
        	Initials.InitGenCo initG = new Initials.InitGenCo(I);
        	GenCoData    = initG.getGenCoData();
        	LearningData = initG.getLearningData(); 
        	  
        	for(int i=0; i<I; i++){                        
        		GenCo gen = new GenCo(GenCoData, LearningData, i, -890999481, Switch, this);        	
        		schedule.scheduleOnce(gen, 1);        	       	
        		//elgrid.addGenCoAtNodeK(gen,(i+1));            
      		//GenCoList.add(gen);        		
        	}             	
//----- LSE-------------------------------------------------------- 
        
        	LSEList = new ArrayList();
        	LSEData = new double[J][26];
        	LoadProfile = new double[J] ;
        	
        	
        	Initials.InitLSE initL = new Initials.InitLSE(J);
        	LSEData = initL.getLSEData();
        	
        	for(int j=0; j<J; j++){                                		
        		LSE lse = new LSE(LSEData, j, this);        	        		
       		    schedule.scheduleOnce(lse, 2);
       		    //elgrid.addLSEAtNodeK(lse,(j+1));            
        		//	LSEList.add(lse);
                	
        	}

//----- ISO-------------------------------------------------------- 
        	
        		LMPList = new ArrayList();
        	
        		ISO iso = new ISO(I, J, this);
            	schedule.scheduleOnce(iso, 3);
        	
        

    
//----- GenCo--------------------------------------------------------        
    
    
	 			for(int i=0; i<I; i++){                        
	 				GenCo gen = new GenCo(LMPLi, Switch, this);        	
	 				schedule.scheduleOnce(gen, 4);      
	 			}
    
	 			}
    
//-------Main-----------------------------------------------------
    public static void main(String[] args)
        {
        Market market = new Market(System.currentTimeMillis());
        
        //market.start(System.currentTimeMillis());
        market.start();
        long steps = 0;
        //while(steps < 5000)
        
        for(int i=0; i<1; i++) {

        	market.schedule.step(market);
            steps = market.schedule.getSteps();

           // System.out.println("Steps: " + steps );
            
            }
        
        //OptimalPowerFlow opf = new OptimalPowerFlow();
        System.out.println("Done!!!");
        market.finish();
        System.exit(0);
        }
    
//---------------------------------------------------------------   
   
    public void setLSEList(Object lselist){
    	LSEList.add(lselist);
    	
    }
    
    public void setLoadProfile(double LP, int jj){
 
    	LoadProfile[jj] = LP;
    	//System.out.println(jj + "  "+LoadProfile[jj][i]);
    } 
    
    public double[] getLoadProfile(){
    	return LoadProfile;
    }
    
    public void setTSupplyOffer(double[] tsupplyoffer, int ii){
    	for (int i=0; i<4; i++){
    	TSupplyOffer[ii][i] = tsupplyoffer[i]; 	
    	}
    }
    
    public void setRSupplyOffer(double[] rsupplyoffer, int ii){
    	for (int i=0; i<4; i++){
    	RSupplyOffer[ii][i] = rsupplyoffer[i]; 	
    	}
    }
    
    public double[][] getRSupplyOffer(){
    	return RSupplyOffer;
    }
    
    
    
    public double[][] getTSupplyOffer(){
    	return TSupplyOffer;
    }
   
    public void addAactionDomain(ArrayList adlist){    	
    	ADList.add(adlist);
    }
    
    public void setLearner(SimpleStatelessLearner learner){    	
    	Learner = learner;
    }
    
    public void addGenCo(Object gencolist ){    	
    	GenCoList.add(gencolist);
    }
    public ArrayList getGenCo(){    	
    	return GenCoList;
    }
    
    public void setLMPList(ArrayList lmplist ){    	
    	LMPList = lmplist;
    }
    
    }
